Skip to main content

로또의 최고 순위와 최저 순위

Solution

function solution(lottos, win_nums) {
// 등수 관련 정의
const RANK_TABLE = [6, 6, 5, 4, 3, 2, 1];

// Set으로 변환 -> 체크가 쉬워짐
const lottosSet = new Set(win_nums);

// 변수 정의
let correctCount = 0;
let zeroCount = 0;

// 한번에 루프 돌면서 0의 개수와 당첨된 번호 체크
lottos.forEach((lotto) => {
if (lotto === 0) zeroCount++;
else if (lottosSet.has(lotto)) correctCount++;
});

// 등수로 변환 후에 리턴
return [RANK_TABLE[correctCount + zeroCount], RANK_TABLE[correctCount]];
}

Review

.

References